const MO = require('../utils/mongoose')
const App = require("../models/cashback")

module.exports = {
    findOne:(params,populate) =>{
        return new Promise(async (resolve,reject) =>{
            MO.findOne(App,params,populate)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       })
    },
    // 获取列表
    getList:params =>{
        return new Promise(async (resolve,reject) =>{
            MO.find(App,params)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       })
    },

    /**
     * 批量添加
     * @param {*} params 对象数组
     * @returns 
     */
    add:params =>{
        return new Promise(async (resolve,reject) =>{
            MO.add(App,params)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       })
    },

    /**
     * 更新数据
     * @param {*} conditions 指定需要更新的数据，对象 
     * @param {*} params 更新参数
     * @returns 
     */
    update:(conditions,param) =>{
        return new Promise(async (resolve,reject) =>{
            MO.update(App,conditions,param)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       }) 
    },
    /**
     * 
     * 根据id更新数据
     * @param {*} id 
     * @param {*} params 
     * @returns 
     */
    updateById:(id,params) =>{
        return new Promise(async (resolve,reject) =>{
            MO.edit(App,id,params)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       }) 
    },

    // 删除
    delete:params =>{
        return new Promise(async (resolve,reject) =>{
            MO.deleteOne(App,params)
            .then(res =>{
                resolve(res)
            })
            .catch(err =>{
                resolve(err)
            })
       })
    },
    
    // 获取订单列表
    getList2:(params,page,sort) =>{
        !sort && (sort = {createTime:-1})
        return MO.aggregate(App,params,page,sort)
    }
}
